<template>
    <div>
        <!-- 退费申请:fzTjsq-->
        <div class="w_100 headTips d_f j_c_s_b">
            <div class="d_f a_i_c">
                <div class="line"></div>
                {{ name }}
            </div>
            <div class="isClose" @click="close">
                <el-icon v-if="type == 'smallForm'">
                    <CloseBold />
                </el-icon>
            </div>
            <el-switch v-if="type == 'bigForm'" @change="change" v-model="status"
                style="--el-switch-on-color: #13ce66;" />
                <el-button type="primary" v-if="type == 'useForm'&&isReset==1" @click="clearForm">清除表单</el-button>
        </div>
        <div class="formBox fzTjsq">

            <div class="fzTjsq_form">
                <myform :fields="fields" ref="form1" :rules="rules" :model="model.releaseApply" :options="options"
                    class="m_t_16">
                </myform>
            </div>

            <div class="segment" v-if="fields1.length > 0"></div>
            <div class="formArrow d_f a_i_c m_t_30" v-if="fields1.length > 0">
                <img src="../../assets/formArrow.png" alt="">
                <span>{{ subTitle || '新加字段' }}</span>
            </div>
            <div v-if="fields1.length > 0">
                <myform :fields="fields1" :model="model.releaseApply" :options="options" class="m_t_16">
                </myform>
            </div>
        </div>
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount, ref } from 'vue'
import { fzTjsqType } from './data'
import { CloseBold } from '@element-plus/icons-vue'
import { copy } from '../../utils/date'
import { formGet, unitFormSet } from '../../api/system/flowPath'
import { listByParentArr } from '../../api/login'
import { Message } from '../../utils/resetMessage'
import { useRouter } from 'vue-router'
import { NUMBRT_REG, INTEGER } from '../../utils/regular'
const model = {
    releaseApply: {
        agencyName: '',//合作机构
        guaMoney: '',//担保金额
        period: '',//期限
        receivablePremium: '',//应收保费
        checkStart: '',//担保起始日期
        checkEnd: '',//担保结束日期
        releaseDate: '',//发起退费日期
        completAccTime: '',//已计收入期限
        completMoney: '',//已计收入
        noCountMoney: '',//未计收入
        releasePremium: '',//应退保费
        approvalRemark: '',//备注
        isReview: 1
    },
}
export default defineComponent({
    name: '',
    props: {
        type: {
            type: String,
            default: '',
        },
        unitId: {
            type: [String, Number],
            default: '',
        },
        isEnabled: {
            type: Number,
            default: '',
        },
        nodeObj: {
            type: Object,
            default: {}
        },
        vars: {
            type: Object,
            default: {}
        },
        isAudit: {
            type: Boolean,
            default: true
        }
    },
    components: {
        CloseBold
    },
    setup(props: any) {
        const data: fzTjsqType = reactive({
            model: copy(model),
            status: true,
            id: '',
            name: '退费申请',
            subTitle: '',
            options: {
            },
            fields: [
                {
                    label: "合作机构",
                    prop: 'agencyName',
                    placeholder: '请输入合作机构',
                    type: 'input',
                    labelWidth: '200px',
                    disabled:false,
                },
                {
                    label: "担保金额",
                    prop: 'guaMoney',
                    placeholder: '请输入担保金额',
                    type: 'input',
                    labelWidth: '200px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "期限",
                    prop: 'period',
                    placeholder: '请输入期限',
                    type: 'number',
                    labelWidth: '200px',
                    unit: '月',
                    disabled:false,
                },
                {
                    label: "应收保费",
                    prop: 'receivablePremium',
                    placeholder: '请输入应收保费',
                    type: 'input',
                    labelWidth: '200px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "担保起始日期",
                    prop: 'checkStart',
                    placeholder: '请选择担保起始日期',
                    type: 'date',
                    labelWidth: '200px',
                    disabled:false,
                },

                {
                    label: "担保到期日期",
                    prop: 'checkEnd',
                    placeholder: '请选择担保到期日期',
                    type: 'date',
                    labelWidth: '200px',
                    disabled:false,
                },
                {
                    label: "发起退费日期",
                    prop: 'releaseDate',
                    placeholder: '请选择发起退费日期',
                    type: 'date',
                    labelWidth: '200px',
                    disabled:false,
                },
                {},
                {
                    label: "已计收入期限",
                    prop: 'completAccTime',
                    placeholder: '请输入已计收入期限',
                    type: 'number',
                    labelWidth: '200px',
                    unit: '月',
                    disabled:false,
                },
                {
                    label: "已计收入",
                    prop: 'completMoney',
                    placeholder: '请输入已计收入',
                    type: 'input',
                    labelWidth: '200px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "未计收入",
                    prop: 'noCountMoney',
                    placeholder: '请输入未计收入',
                    type: 'input',
                    labelWidth: '200px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "应退保费",
                    prop: 'releasePremium',
                    placeholder: '请输入应退保费',
                    type: 'input',
                    labelWidth: '200px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "备注（选填）",
                    prop: 'approvalRemark',
                    placeholder: '请输入备注',
                    type: 'textarea',
                    labelWidth: '200px',
                    disabled:false,
                },
            ],
            fields1: [],
            rules: {
                agencyName: [
                    { required: true, message: '请输入合作机构', trigger: 'blur' },
                ],
                guaMoney: [
                    { required: true, message: '请输入担保金额', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '担保金额格式不正确' }
                ],
                period: [
                    { required: true, message: '请输入期限', trigger: 'blur' },
                    { pattern: INTEGER, message: '期限格式不正确' }
                ],
                receivablePremium: [
                    { required: true, message: '请输入应收保费号', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '应收保费格式不正确' }
                ],
                checkStart: [
                    { required: true, message: '请选择担保起始日期', trigger: 'change' },
                ],
                checkEnd: [
                    { required: true, message: '请选择担保到期日期', trigger: 'change' },
                ],
                releaseDate: [
                    { required: true, message: '请选择发起退费日期', trigger: 'change' },
                ],
                completAccTime: [
                    { required: true, message: '请输入已计收入期限', trigger: 'blur' },
                    { pattern: INTEGER, message: '期限格式不正确' }
                ],
                completMoney: [
                    { required: true, message: '请输入已计收入', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '已计收入格式不正确' }
                ],
                noCountMoney: [
                    { required: true, message: '请输入未计收入', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '未计收入格式不正确' }
                ],
                releasePremium: [
                    { required: true, message: '请输入应退保费', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '应退保费格式不正确' }
                ],
            },
            isReset:2,
        })
        const router = useRouter()
        const form1 = ref()
        onBeforeMount(() => {
            methods.getData()
            data.status = props.isEnabled == 0 ? false : true
        })
        const methods = {
            close() {
                router.push('/system/flowPath/childForm/index')
            },
            //获取自定义表单
            getData() {
                formGet({ tag: 'fzTjsq' }).then((res: any) => {
                    data.id = res.id
                    data.name = res.name
                    data.isReset=res.isReset
                    if (!res.formStruct || res.formStruct == 'null' || res.formStruct == '{}') return
                    data.subTitle = res.subTitle
                    let formStruct = JSON.parse(res.formStruct)
                    formStruct.formFields.map((item:any)=>{
                        data.model.releaseApply[item.prop]=''
                    })
                    data.fields1 = formStruct.formFields
                    data.fields.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    data.fields1.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    if (props.vars) {
                        const localData = props.vars
                        for (let key in data.model.releaseApply) {
                            if(localData.releaseApply[key]!=null){
                                data.model.releaseApply[key] = localData.releaseApply[key]
                            }
                        }
                        if(localData.releaseApply.agencyBranchName){
                            data.model.releaseApply.agencyName=data.model.releaseApply.agencyName+'/'+localData.releaseApply.agencyBranchName
                        }
                        if(props.type=='useForm'){
                          data.model.releaseApply.approvalRemark=''
                        }
                    } 
                    let arr = formStruct.formFields.filter((item: any) => {
                        return item.type == 'select'
                    })
                    let ids = arr.map((item: any) => {
                        return item.prop
                    })
                    if (ids.length == 0) return
                    listByParentArr({ ids: ids }).then((res: any) => {
                        ids.map((item: any) => {
                            data.options[item] = res[item]
                        })
                    })
                })
            },
            //设置表单的启用跟停用
            change() {
                unitFormSet({ isEnabled: data.status ? 1 : 0, id: data.id, unitId: props.unitId }).then(() => {
                    Message({ type: 'success', message: '设置成功' })
                }).catch(() => {
                    data.status = !data.status
                })
            },
            //清除表单数据
            clearForm() {
                data.model = copy(model)
            },
            resultData() {
                return new Promise((resolve, reject) => {
                    form1.value.verification().then((res) => {
                        resolve(data.model)
                    }).catch(() => {
                        reject(false)
                    })
                })
            },
            saveData() {
                return new Promise((resolve, reject) => {
                    resolve(data.model)
                })
            },
        }
        return {
            ...toRefs(data),
            ...methods, form1
        }
    }
})
</script>
<style lang='scss'>
.fzTjsq {
    .el-form-item {
        width: 50% !important;
        margin-right: 0px !important;

        .el-form-item__content {
            width: calc(100% - 200px) !important;

            .el-input,
            .el-cascader,
            .el-select {
                width: 100% !important;
            }
        }
    }
}

.fzTjsq_form {
    .el-form-item:nth-child(13) {
        width: 100% !important;
    }
}
</style>